package com.fs.user.web;

import com.fs.entity.User;
import com.fs.service.UserService;
import com.fs.service.impl.UserServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet( "/AddUserServlet")
public class AddUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 1.创建FileItem工厂对象
         */
        DiskFileItemFactory  factory = new DiskFileItemFactory();
        /**
         * 2.创建解析器对象
         */
        ServletFileUpload servletFileUpload = new ServletFileUpload(factory);

        try {
            /**
             * 3.解析Request对象
             */
            List<FileItem> fileItems = servletFileUpload.parseRequest(request);
            User user = new User();
            for (FileItem fileItem : fileItems) {
                if(fileItem.isFormField()){//普通表单子项, 非文件域
                    if(fileItem.getFieldName().equals("name")){
                        user.setName(fileItem.getString("UTF-8"));
                    }
                    if(fileItem.getFieldName().equals("sex")){
                        user.setGender(fileItem.getString("UTF-8"));
                    }
                    if(fileItem.getFieldName().equals("age")){
                        user.setAge(Integer.parseInt(fileItem.getString("UTF-8")));
                    }
                    if(fileItem.getFieldName().equals("address")){
                        user.setAddress(fileItem.getString("UTF-8"));
                    }
                    if(fileItem.getFieldName().equals("qq")){
                        user.setQq(fileItem.getString("UTF-8"));
                    }
                    if(fileItem.getFieldName().equals("email")){
                        user.setEmail(fileItem.getString("UTF-8"));
                    }
                }else{//文件域
                    //文件上传
                    String fileName = fileItem.getName();
                    File savePath = new  File("D:/test");
                    if(!savePath.exists()){
                        savePath.mkdirs();
                    }
                    //生成一个唯一的文件名
                    String newFileName = UUID.randomUUID().toString().toUpperCase().replace("-", "");
                    //得到后缀名
                    String suffix =  fileName.substring(fileName.lastIndexOf("."));

                    fileItem.write(new File(savePath,newFileName+suffix));
                    //给user的photo属性赋值  文件名
                    user.setPhoto(newFileName+suffix);
                }
                
            }
            //保存用户到数据库
            UserService userService = new UserServiceImpl();
            userService.addUser(user);

            //重定向到首页  index.jsp
            response.sendRedirect("/index.jsp");

        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
